<?php

namespace App\Http\Controllers\Resource;

use Illuminate\Support\Facades\Log;

/**
 * 云图有声 数字资源
 *
 * 提供目录接口
 */
class YtysController extends CommonController
{
    public $md5_key = 'wBIHCpsKFznU4e2v8OXWvAve8fwCBzEe';
    public $only_sign = 'T2lUcGJnQ3IwYkdCRzc4N3lGZE13WXZQNGdPZFRxOEgvZ3pTYjhOVWptQ3d0WWp3bzNWd3Z3PT0';
    public $class_type_id = '1469'; //只能获取此类数据
    public $host_url = 'https://open-service.yuntuys.com';
    public function __construct()
    {
        parent::__construct();
    }

    /**
     * 分类接口-根据父级编号获取下级分类
     *
     * classTypePid 备注：classTypePid =1469
     */
    public function getType()
    {
        $url = $this->host_url . '/api_v2/org/class_type/getClassTypeByPid/' . $this->class_type_id;
        $result = request_url($url);
        $result = json_decode($result, true);
        if ($result['code'] !== 0) {
            Log::channel('ytyslog')->debug('资源获取失败');
            Log::channel('ytyslog')->debug($result);
            Log::channel('ytyslog')->debug('--------------------------------------------------------');
            return []; //没数据时，返回的false
        }
        return  $result;
    }


    /**
     * 分类接口-获取书籍分类的关系
     */
    public function getBookTypeRelation()
    {
        $url = $this->host_url . '/api_v2/org/class_type/getBookClassTypeByPid/' . $this->class_type_id;

        $result = request_url($url);
        $result = json_decode($result, true);
        if ($result['code'] !== 0) {
            Log::channel('ytyslog')->debug('资源获取失败');
            Log::channel('ytyslog')->debug($result);
            Log::channel('ytyslog')->debug('--------------------------------------------------------');
            return []; //没数据时，返回的false
        }
        if (empty($result['data'])) {
            return [];
        }
        return  $result;
    }

    /**
     * 书籍接口-获取分类下包含的书籍（分页）接口数据都是上架数据需进行剔除
     * @param  classTypeId
     * @param  sign MD5签名
     * @param  pageNum 页码
     * @param  pageSize 每页大小
     */
    public function getTypeBookList($page = 1, $limit = 10)
    {
        // $this->class_type_id = 1470;
        $sign = $this->getSign($this->class_type_id);
        $url = $this->host_url . '/api_v2/org/book/getBookListByType/' . $this->class_type_id . '?sign=' . $sign . '&pageNum=' . $page . '&pageSize=' . $limit;

        $result = request_url($url);
        $result = json_decode($result, true);
        if ($result['code'] !== 0) {
            Log::channel('ytyslog')->debug('资源获取失败');
            Log::channel('ytyslog')->debug($result);
            Log::channel('ytyslog')->debug('--------------------------------------------------------');
            return []; //没数据时，返回的false
        }
        if (empty($result['data']['list'])) {
            return [];
        }
        return  $result;
    }

    /**
     * 书籍接口-根据书籍编号获取书籍信息
     * @param  book_id
     * @param  sign MD5签名
     */
    public function getBookInfoByBookId($book_id)
    {
        $sign = $this->getSign($book_id);
        $url = $this->host_url . '/api_v2/org/book/getBook/' . $book_id . '?sign=' . $sign;

        $result = request_url($url);
        $result = json_decode($result, true);

        if ($result['code'] !== 0) {
            Log::channel('ytyslog')->debug('资源获取失败');
            Log::channel('ytyslog')->debug('书籍ID:' . $book_id);
            Log::channel('ytyslog')->debug($result);
            Log::channel('ytyslog')->debug('--------------------------------------------------------');
            return []; //没数据时，返回的false
        }
        return  $result;
    }

    /**
     * 章节接口-根据书籍ID获取书籍章节信息（分页）
     * @param  book_id
     * @param  sign MD5签名
     * @param  pageNum 页码
     * @param  pageSize 每页大小
     */
    public function getBookCatalogue($book_id, $page = 1, $limit = 10)
    {
        $sign = $this->getSign($book_id);
        $url = $this->host_url . '/api_v2/org/book/selectChapters/' . $book_id . '?sign=' . $sign . '&pageNum=' . $page . '&pageSize=' . $limit;

        $result = request_url($url);
        $result = json_decode($result, true);
        if ($result['code'] !== 0) {
            Log::channel('ytyslog')->debug('资源获取失败');
            Log::channel('ytyslog')->debug('书籍ID:' . $book_id);
            Log::channel('ytyslog')->debug($result);
            Log::channel('ytyslog')->debug('--------------------------------------------------------');
            return []; //没数据时，返回的false
        }
        if (empty($result['data']['list'])) {
            return [];
        }
        $data = [];
        foreach ($result['data']['list'] as $key => $val) {
            $data['data'][$key]['resource_business_id'] = 3;
            $data['data'][$key]['book_id'] = $val['bookId'];
            $data['data'][$key]['sort'] = $val['rank']; //排序
            $data['data'][$key]['chapter_id'] = $val['chapterid'];
            $data['data'][$key]['chapter_name'] = $val['name'];
            $data['data'][$key]['chapter_duration'] = null; //时长
            $data['data'][$key]['chapter_size'] = null; //大小
        }
        $data['total'] = $result['data']['totalRow'];
        $data['per_page'] = $result['data']['totalPage'];
        $data['current_page'] = $result['data']['pageNumber'];
        return  $data;
    }

    /**
     * 音频接口-获取章节有效的URL
     * @param  chapter_id
     * @param  sign MD5签名
     */
    public function getAudioInfo($chapter_id)
    {
        $sign = $this->getSign($chapter_id);
        $url = $this->host_url . '/api_v2/org/book/getChapterByAudioUrl/' . $this->only_sign . '?chapterid=' . $chapter_id . '&sign=' . $sign;

        $result = request_url($url);
        $result = json_decode($result, true);
        if ($result['code'] !== 0) {
            Log::channel('ytyslog')->debug('资源获取失败');
            Log::channel('ytyslog')->debug('章节ID:' . $chapter_id);
            Log::channel('ytyslog')->debug($result);
            Log::channel('ytyslog')->debug('--------------------------------------------------------');
            return []; //没数据时，返回的false
        }
        return  $result;
    }

    /**
     * 获取全量下架资源
     */
    public function getSoldOut()
    {
        $url = $this->host_url . '/api/org/book/selectUnBooks';

        $result = request_url($url);
        $result = json_decode($result, true);
        Log::channel('ytyslog')->debug($result);
        if ($result['code'] !== 0) {
            Log::channel('ytyslog')->debug('资源获取失败');
            Log::channel('ytyslog')->debug('--------------------------------------------------------');
            return []; //没数据时，返回的false
        }
        return  $result;
    }


    /**
     * 获取签名
     */
    public function getSign($id)
    {
        return md5($id . $this->md5_key . date('Y-m-d'));
    }
}
